<?php
class FacebookApi extends AbsApi {
	public function setOauth($oauth_request_token = '', $oauth_request_secret = '') {
		$this->oauth = new Facebook(array(
			'appId'  => API_FACEBOOK_APPID,
			'secret' => API_FACEBOOK_APPSECRET,
			'cookie' => true,
		));
	}
	
	//返回由oauth_request_token和oauth_request_secret组成的数组
	public function getRequestToken() {
		return array(
			'oauth_token' => '',
			'oauth_token_secret' => '',
		);
	}
	
	public function getAuthorizeURL($oauth_request_token, $sign_in = false, $callback = '') {
		return $this->oauth->getLoginUrl(
			array(
				'next' => API_FACEBOOK_LOGIN_CALLBACK,
				'display' => 'page', //如果是弹窗请选择'popup'
			)
		);
	}
	
	//调用这个方法时setOauth的两个参数要传递
	public function getAccessToken($oauth_verifier) {
		return array(
			'oauth_token' => $this->oauth->getSession(),
			'oauth_token_secret' => '',
		);
	}
	
	public function setClient($oauth_access_token, $oauth_access_secret) {
		if(!isset($this->client)) {
			$this->client = new Facebook(array(
				'appId'  => API_FACEBOOK_APPID,
				'secret' => API_FACEBOOK_APPSECRET,
				'cookie' => true,
			));
			
			$this->client->setSession($oauth_access_token);
		}
	}
	
	//调用api函数之前要先调用setClient
	public function api() {
		$args = func_get_args();
		$method = array_shift($args);
		return call_user_func_array(array($this->client, $method), $args);
	}
	
	//facebook的登出连接要特殊对待
	public function getLogoutUrl() {
		return $this->client->getLogoutUrl(
			array(
				'next' => API_FACEBOOK_LOGOUT_CALLBACK,
			)
		);
	}
}
?>